/*
File Name:      uknow_events.js
Created By:     victor.liu
Created When:   2012-02-10
Created Whys:   This js file is created mainly for eventcalendar.html
*/

function Events(langID, id) {

};

function EventsComment(langID, id) { };

Events.gateUrl = "/GateService/EventGate.aspx";

Events.parseShowDate = function (langID, startDate, endDate, interval) {
    var gateUrl = "/GateService/EventGate.aspx?lang=" + langID + "&op=parseShowDate&sd=" + startDate + "&ed=" + endDate + "&interval=" + interval + "&ram=" + new Date().toLocaleString();
    var req = new ajaxRequest(gateUrl);
    req.async = false;
    var showDate = "";
    req.get(function (res) {
        showDate = res.ReturnObject.toString();
    });

    return showDate;
};

Events.getFilePath = function (langID) {
    var root = (langID == 2052) ? "/cn" : "/en";
    root += "/events";
    return root;
};

Events.getFileName = function (evt) {
    var dir = Events.getFilePath(evt.LanguageID);
	var name = (evt.HtmlFileName == null || typeof evt.HtmlFileName == "undefined" || evt.HtmlFileName.trim().length == 0) ? evt.ID + ".html" : evt.HtmlFileName;
	return dir + "/" + name;
};

Events.getRenderHtml = function (evt) {
    var li = $('<li></li>').attr("class", evt.ArtForm.Css);

    var dl = $('<dl></dl>');
    var imgSrc = (evt.ListImg == null || evt.ListImg == "") ? Environment.FileName.DefaultEventListImage : Environment.FilePath.UploadImagePath + "/" + evt.ListImg;
    var dt = $('<dt></dt>');//.html('<img src="' + imgSrc + '" alt="">');
    var a = $('<a></a>').attr('href', Events.getFileName(evt)).html('<img src="' + imgSrc + '" alt="">');
    dt.append(a);
    var dd = $('<dd></dd>');
    var innerDl = $('<dl></dl>');
    var innerDt = $('<dt></dt>').html('<a href="' + Events.getFileName(evt) + '">' + evt.Title.replace(/&lt;/g, '<').replace(/&gt;/g, '>') + '</a>');
    var cityName = (g_langID == 2052) ? evt.CityModel.Cnname : evt.CityModel.Enname;
    var showTime = evt.ShowTime;
    if (showTime == null || typeof showTime == "undefined" || showTime == "")
        showTime = eventsConstants.emptyShowTime;

    var innerDd = $('<dd></dd>').html(showTime + '<span>' + cityName + ' / ' + evt.ArtForm.Name + '</span>');
    innerDl.append(innerDt).append(innerDd);
    dd.append(innerDl);
    dl.append(dt).append(dd);
    li.append(dl);

    return li;
};

Events.getModelByID = function (langID, id) {
    var url = Events.gateUrl + "?lang=" + langID + "&op=GetEventsByID&eid=" + id + "&ram=" + new Date().toLocaleString();
    var req = new ajaxRequest(url);
    req.async = false;
    var model = null;
    req.get(function (res) {
        model = res;
    });

    return model;
};

var getEventByID = function () {
    var langID = g_langID;
    var eventsIDCtrl = document.getElementById("hdn_eventsId");
    var eventsID = null;
    if (eventsIDCtrl != null && typeof eventsIDCtrl != 'undefined')
        eventsID = eventsIDCtrl.value;

    if (eventsID == null) return;

    var model = Events.getModelByID(langID, eventsID);
    return model;
};

var getEventCommentByID = function (cmtID) {
    var langID = g_langID;
    var eventsIDCtrl = document.getElementById("hdn_eventsId");
    var eventsID = null;
    if (eventsIDCtrl != null && typeof eventsIDCtrl != 'undefined')
        eventsID = eventsIDCtrl.value;

    if (eventsID == null) return;

    var url = Events.gateUrl + "?lang=" + langID + "&op=GetEventsCommentByID&eid=" + eventsID + "&cid=" + cmtID + "&ram=" + new Date().toLocaleString();
    var req = new ajaxRequest(url);
    req.async = false;
    var model = null;
    req.get(function (res) {
        model = res;
    });
    return model;
};

var shareSina = function (cmtID) {
    var model = (cmtID != null && cmtID != 'undefined') ? getEventCommentByID(cmtID) : getEventByID();
    if (model == null) return;
    var params = (cmtID != null && cmtID != 'undefined') ? EventsComment.buildShareParams(model) : Events.buildShareParams(model);
    sharesina(params.title, params.pic);
};

var shareRenren = function (cmtID) {
    var model = (cmtID != null && cmtID != 'undefined') ? getEventCommentByID(cmtID) : getEventByID();
    if (model == null) return;
    var params = (cmtID != null && cmtID != 'undefined') ? EventsComment.buildShareParams(model) : Events.buildShareParams(model);
    sharerenren(params.title, params.content, params.pic);
}

var shareQQ = function (cmtID) {
    var model = (cmtID != null && cmtID != 'undefined') ? getEventCommentByID(cmtID) : getEventByID();
    if (model == null) return;
    var params = (cmtID != null && cmtID != 'undefined') ? EventsComment.buildShareParams(model) : Events.buildShareParams(model);
    shareqq(params.title, params.content, params.pic);
}

var shareKaixin = function (cmtID) {
    var model = (cmtID != null && cmtID != 'undefined') ? getEventCommentByID(cmtID) : getEventByID();
    if (model == null) return;
    var params = (cmtID != null && cmtID != 'undefined') ? EventsComment.buildShareParams(model) : Events.buildShareParams(model);
    sharekaixin001(params.title, params.content, params.pic);
}

var shareDouban = function (cmtID) {
    var model = (cmtID != null && cmtID != 'undefined') ? getEventCommentByID(cmtID) : getEventByID();
    if (model == null) return;
    var params = (cmtID != null && cmtID != 'undefined') ? EventsComment.buildShareParams(model) : Events.buildShareParams(model);
    sharedouban(params.title, params.content, params.pic);
}

Events.buildShareParams = function (evt) {
    // 获取活动图片
    var url = Events.gateUrl + "?lang=" + evt.LanguageID + "&op=GetEventsImagesByEventsID&eid=" + evt.ID + "&ram=" + new Date().toLocaleString();
    var req = new ajaxRequest(url);
    req.async = false;
    var images = null;
    req.get(function (res) {
        images = res;
    });

    var length = images.length;
    var pics = "";
    for (var i = 0; i < length; i++) {
        if (pics.length > 0)
            pics += ",";
        pics += "http://www.uknow.org.cn" + Environment.FilePath.UploadImagePath + "/" + images[i].Path;
        break;
    }

    var params = {
        title: evt.Title,
        content: evt.ShortDescription,
        pic: pics
    };

    return params;
};

EventsComment.buildShareParams = function (cmt) {
    var pics = "http://www.uknow.org.cn" + Environment.FilePath.UploadFrontImagePath + "/" + cmt.ImageName;

    var params = {
        title: "",
        content: cmt.Content,
        pic: pics
    };

    return params;
};